import {_decorator, instantiate, Prefab} from 'cc';
import UCard from '../components/UCard';
import {BaseCard} from '../models/Card';
import {ClickEventType} from '../GameDef';
import {ManagerBase} from './ManagerBase';
import {CardPoolMgr} from './CardPoolMgr';

const {ccclass, property} = _decorator;

//手牌结构组件集合,管理若干张手牌
@ccclass('CardsHandMgr')
export default class CardsHandMgr extends ManagerBase {
    _cards: BaseCard[] = [];

    @property(Prefab)
    cardPrefab: Prefab = null;



    get cardsUI(): UCard[] {
        return this.node.children.map(node => node.getComponent(UCard));
    }

    onLoad() {
        
    }

    initData(): void {
        this._cards = CardPoolMgr.getInstance().getUnusedCards();
        // this._cards.sort((a, b) => {
        //     return Math.random() > 0.5 ? 1 : -1;
        // })
    }
    public initUI() {
        this.refreshUI();
    }

    public refreshUI(): void {

        this.node.destroyAllChildren();
        if(this._cards.length == 0)
            return;
        for (let originalCardData of this._cards) {
            let cardNode = instantiate(this.cardPrefab);
            this.node.addChild(cardNode);
            let card = cardNode.getComponent(UCard);
            card.init(0,0,ClickEventType.HAND);
            card.refresh(originalCardData);
        }

        this.resetCardsPos();
    }

    resetCardsPos() {

        for (let i = 0; i < this.cardsUI.length; i++) {
            let card = this.cardsUI[i];
            if (i != 0) {
                card.node.setPosition((i - 1) * 20, 0);
            } else {
                card.node.setPosition(500, 0);
                card.isOpen = true;
            }
        }
    }

    getCardUIByUid(uid) {
        for (let card of this.cardsUI) {
            if (card.uid == uid) {
                return card;
            }
        }
    }

    tips(needCards:number[]):boolean{
        const openCard = this.getOpenCard();
        let ret = needCards.indexOf(openCard.uid)
        if (ret >= 0) {
            if (openCard) {
                openCard.select();
                return true;
            }
        }
        return false;
    }

    removeCardByUid(uid) {
        let cardUI = this.getCardUIByUid(uid);
        if(cardUI){
            cardUI.node.removeFromParent();
            this.resetCardsPos()
            return;
        }
    }

    correctCard(cardData: BaseCard) {
        let cardUI = this.getCardUIByUid(cardData.uid);
        if(cardUI) {
            console.log('correctCard cardMountain',cardUI.uid,'to',cardData.uid);
            cardUI.refresh(cardData);
        }
    }

    public shuffle(needCardUids:number[]) {
        if(needCardUids.length == 0)
            return;
        let count =0;
        for(let uid of needCardUids) {
            let needCard = this.cardsUI.find(card => card.uid == uid);
            if(!needCard)
                continue;
            let switchCard = this.cardsUI[count];
            let switchCardData = CardPoolMgr.getInstance().getCardById(switchCard.uid);
            let needCardData = CardPoolMgr.getInstance().getCardById(needCard.uid);
            switchCard.refresh(needCardData);
            needCard.refresh(switchCardData);
            count++;
        }

    }

    showAllCards() {
        for(let card of this.cardsUI) {
            card.isOpen = true;
        }
    }

    private getOpenCard() {
        return this.cardsUI[0];
    }
}